import { defineStore } from 'pinia'
import { ref } from 'vue'

// 缓存管理store
export const useCacheStore = defineStore(
	'sms-cache',
	() => {
		// 保存需要缓存的路由组件名称列表
		const cachedRoutes = ref<string[]>([])

		// 添加需要缓存的路由组件
		const addCachedRoute = (routeName: string) => {
			if (!cachedRoutes.value.includes(routeName)) {
				cachedRoutes.value.push(routeName)
			}
		}

		// 移除缓存的路由组件
		const removeCachedRoute = (routeName: string) => {
			const index = cachedRoutes.value.indexOf(routeName)
			if (index > -1) {
				cachedRoutes.value.splice(index, 1)
			}
		}

		// 清空所有缓存
		const clearCachedRoutes = () => {
			cachedRoutes.value = []
		}

		return {
			cachedRoutes,
			addCachedRoute,
			removeCachedRoute,
			clearCachedRoutes
		}
	},
	{
		persist: true // 开启持久化
	}
)
